package musicstore.com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import musicstore.com.dao.AlbumDao;
import musicstore.com.dao.GenreDao;
import musicstore.com.dao.impl.AlbumDaoImpl;
import musicstore.com.dao.impl.GenreDaoImpl;
import musicstore.com.entity.Album;
import musicstore.com.entity.Genre;

public class InquireServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		GenreDao genreDao = new GenreDaoImpl();
		List<Genre> genreList = genreDao.getGenreList();
		AlbumDao albumDao= new AlbumDaoImpl();
		int gid = 0;
		if(request.getParameter("gid")!=null && !request.getParameter("gid").isEmpty())
			gid = Integer.parseInt(request.getParameter("gid"));
		String title = "";
		if(request.getParameter("title")!=null )
			title = request.getParameter("title");
		System.out.println(title);
		int pageSize = 3 ;
		int pageNum = 1;
		if(request.getParameter("page")!=null && !request.getParameter("page").isEmpty())
			pageNum=Integer.parseInt(request.getParameter("page"));
		int rows = albumDao.getAlbumRows(gid, title);
		int totalPage = (int)Math.ceil((double)rows/pageSize);

		List<Album> albumList = albumDao.getAll(gid, title, pageSize, pageNum);
		
		request.setAttribute("genre", genreList);
		request.setAttribute("album", albumList);
		
		request.getRequestDispatcher("inquire.jsp").forward(request, response);
	}

}
